package task1;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentSystem {
    private List<Student> students;
    private String storeFile;

    public StudentSystem(String storeFile) {
//        setStudents(students);
        setStoreFile(storeFile);
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public String getStoreFile() {
        return storeFile;
    }

    public void setStoreFile(String storeFile) {
        this.storeFile = storeFile;
        File file = null;
        if(!(file = new File(storeFile)).exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void initFromDisk(){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(storeFile));
            students = (ArrayList<Student>) ois.readObject();
//            }
        } catch (IOException e) {
//            e.printStackTrace();
            students = new ArrayList<Student>();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(null != ois){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void storeObject(){
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(storeFile));
            oos.writeObject(students);
        } catch(IOException e){
            e.printStackTrace();
        } finally {
            if(null != oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public String toString() {
        return students.toString();
    }

    public static void main(String[] args) {
        StudentSystem ss = new StudentSystem("d:/StudentList.txt");
        // 从文件中初始化学生信息list、
        ss.initFromDisk();
        // 获取学生信息list
        List<Student> students = ss.getStudents();

        Scanner scanner = new Scanner(System.in);
//        String[] commands = {"增加", "删除", "修改", "查找", "遍历"};
        while(true){
            System.out.println("请输入命令：增加, 删除, 修改, 查找, 遍历, 退出" );
            String command = scanner.next();
            switch(command){
                case "增加":
                    System.out.println("请输入需要增加的学生信息：学号，姓名，年龄");
                    String id = scanner.next();
                    String name = scanner.next();
                    int age = scanner.nextInt();
                    try {
                        StudentAdd.add(students, id, name, age);
                    } catch (StudentIDException e) {
                        e.printStackTrace();
                    } catch (StudentAgeException e) {
                        e.printStackTrace();
                    }
                    ss.setStudents(students);
                    break;
                case "删除":
                    System.out.println("全部学生信息如下：");
                    System.out.println(students);
                    System.out.println("请输入要删除学生的id");
                    id = scanner.next();
                    StudentDel.delete(students, id);
                    break;
                case "修改":
                    System.out.println("请输入要修改的学生信息：学号，姓名，年龄");
                    id = scanner.next();
                    name = scanner.next();
                    age = scanner.nextInt();
                    StudentMod.modify(students, id, name, age);
                    break;
                case "查找":
                    System.out.println("请输入查找方式：ID， 姓名");
                    String subCommand = scanner.next();
                    if("ID".equals(subCommand)){
                        System.out.println("请输入要查询的ID");
                        StudentSearch.searchById(students, scanner.next());
                    }else if("姓名".equals(subCommand)){
                        System.out.println("请输入要查询的姓名");
                        StudentSearch.searchByName(students, scanner.next());
                    }
                    break;
                case "遍历":
                    System.out.println("全部学生信息如下");
                    StudentLoop.loop(students);
                    break;
                case "退出":
                    ss.storeObject();
                    return;
                default:
                    System.out.println("输入命令不存在");
                    continue;
            }
        }
    }
}
